package com.fishingwithme.domain.spots;

import com.fishingwithme.infrastructure.CodeEnum;
import com.fishingwithme.infrastructure.utils.CommonUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Getter;
import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

/**
 * 钓点类型
 */
@Getter
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum SpotType implements CodeEnum{
    //坎位
    KAN(1, "坎位"),
    HUIWAN(2, "回湾"),
    QIANTAN(3, "浅滩"),
    CHAKOU(4, "岔口"),
    HUAJIAN(5, "划尖"),
    CAODONG(6, "草洞"),
    LUANSHI(7, "乱石堆"),
    ZHANGAI(8, "障碍区"),
    SHUICAO(9, "水草区"),
    KUANZHAI(9, "宽窄区"),
    RUSHUIKOU(11, "入水口"),
    SHENQIANJIAOJIE(12, "深浅交界"),
    JIHUANLIUJIAOJIE(13, "急缓流交界"),
    QITA(14, "其他");

    //钓点
    @JsonProperty("type")
    private int type;
    @JsonProperty("name")
    private String name;

    SpotType(int type, String name) {
        this.type = type;
        this.name = name;
    }


    // 使用静态 Map 提高查找效率
    private static final Map<Integer, SpotType> TYPE_MAP = new HashMap<>();

    static {
        for (SpotType type : values()) {
            TYPE_MAP.put(type.getType(), type);
        }
    }

    // 优化后的查找方法（O(1) 时间复杂度）
    public static SpotType of(int type) {
        return TYPE_MAP.getOrDefault(type, null);
    }

    // 返回 JSON 格式字符串
    @Override
    public String toString() {
        try {
            return toJson().toString();
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    // 返回 JSON 对象
    public ObjectNode toJson() throws JSONException {
        ObjectNode json = CommonUtils.objectMapper.createObjectNode();
        json.put("code", this.name());
        json.put("type", this.type);
        json.put("name", this.name);
        return json;
    }


}
